from .view import GraphView
from . import schema
from typing import List
from fastapi import APIRouter, Depends, Security
from package.connector.sql_db import db
from package.fastapi import permission


router_graph = APIRouter(prefix='/graph', tags=['关系视图'])
router_meta = {'module': '运维管理', 'sub_module': '关系视图'}


@router_graph.get('/', response_model=schema.PageGraphs, openapi_extra=router_meta, name='graph_list_get',
                 summary='关系视图列表')
def graph_get(q: str = None,
             page: int = 1,
             page_size: int = 10,
             order_by: str = 'created_at desc',
             session=Depends(db.get_session),
             user=Security(permission.security, scopes=['graph:read'])):
    return GraphView.get_page_items(session, q=q, page=page, page_size=page_size, order_by=order_by)


@router_graph.post('/', response_model=schema.GraphOut, openapi_extra=router_meta, name='graph_post', summary='关系视图创建')
def graph_post(body: schema.CreateGraph,
              session=Depends(db.get_session),
              user=Security(permission.security, scopes=['graph:write'])):
    return GraphView.create_item(session, body=body.dict())


@router_graph.get('/{id}', response_model=schema.GraphOut, openapi_extra=router_meta, name='graph_get',
                 summary='关系视图详情')
def graph_get(id: str,
             session=Depends(db.get_session),
             user=Security(permission.security, scopes=['graph:read'])):
    return GraphView.get_item(session, filter_=[GraphView.id == id])


@router_graph.patch('/{id}', response_model=schema.GraphOut, openapi_extra=router_meta, name='graph_patch',
                   summary='关系视图修改')
def graph_patch(id: str,
               body: schema.EditGraph,
               session=Depends(db.get_session),
               user=Security(permission.security, scopes=['graph:write'])):
    return GraphView.update_item(session, filter_=[GraphView.id == id], update=body.dict(exclude_unset=True))


@router_graph.delete('/', openapi_extra=router_meta, name='graph_delete', summary='关系视图删除')
def graph_delete(body: schema.BatchId,
                session=Depends(db.get_session),
                user=Security(permission.security, scopes=['graph:write'])):
    return {'count': GraphView.delete_items(session, filter_=[GraphView.id.in_(body.ids)])}


# 节点管理
@router_graph.get('/{graph_id}/node', response_model=List[schema.Node], openapi_extra=router_meta, name='get_graph_node', 
                  summary='关系视图节点获取')
def get_graph_node(graph_id,
                   user=Security(permission.security, scopes=['graph:read'])):
    return GraphView.get_graph_node(graph_id)


@router_graph.post('/{graph_id}/node', response_model=schema.Node, openapi_extra=router_meta, name='post_graph_node', summary='关系视图节点创建')
def post_graph_node(graph_id: str,
                    body: schema.Node,
                    user=Security(permission.security, scopes=['graph:write'])):
    return GraphView.post_graph_node(graph_id, body.dict())


@router_graph.patch('/{graph_id}/node/{node_id}', response_model=schema.Node, openapi_extra=router_meta, name='patch_graph_node', 
                    summary='关系视图节点修改')
def patch_graph_node(graph_id: str,
                     node_id: str,
                     body: schema.Node,
                    user=Security(permission.security, scopes=['graph:write'])):
    return GraphView.patch_graph_node(graph_id, node_id, body.dict())


@router_graph.delete('/{graph_id}/node/{node_id}', openapi_extra=router_meta, name='delete_graph_node', summary='关系视图节点删除')
def delete_graph_node(graph_id: str,
                      node_id: str,
                      user=Security(permission.security, scopes=['graph:write'])):
    return GraphView.delete_graph_node(graph_id, node_id)


# 关系管理
@router_graph.get('/{graph_id}/relation', response_model=List[schema.Relation], openapi_extra=router_meta, name='get_graph_relation', 
                  summary='关系视图关系获取')
def get_graph_relation(graph_id,
                       user=Security(permission.security, scopes=['graph:read'])):
    return GraphView.get_graph_relation(graph_id)


@router_graph.post('/{graph_id}/relation', response_model=schema.Relation, openapi_extra=router_meta, name='post_graph_relation', summary='关系视图关系创建')
def post_graph_relation(graph_id: str,
                        body: schema.Relation,
                        user=Security(permission.security, scopes=['graph:write'])):
    return GraphView.post_graph_relation(graph_id, body.dict())


@router_graph.patch('/{graph_id}/relation/{relation_id}', response_model=schema.Relation, openapi_extra=router_meta, name='patch_graph_relation', 
                    summary='关系视图关系修改')
def patch_graph_relation(graph_id: str,
                         relation_id: str,
                         body: schema.Relation,
                         user=Security(permission.security, scopes=['graph:write'])):
    return GraphView.patch_graph_relation(graph_id, relation_id, body.dict())


@router_graph.delete('/{graph_id}/relation/{relation_id}', openapi_extra=router_meta, name='delete_graph_relation', summary='关系视图关系删除')
def delete_graph_relation(graph_id: str,
                          relation_id: str,
                          user=Security(permission.security, scopes=['graph:write'])):
    return GraphView.delete_graph_relation(graph_id, relation_id)


# 图表整体数据管理
@router_graph.get('/{graph_id}/graph', response_model=schema.GraphElements, openapi_extra=router_meta, name='get_graph_element', 
                  summary='关系视图元素获取')
def get_graph(graph_id,
              user=Security(permission.security, scopes=['graph:read'])):
    return GraphView.get_graph_element(graph_id)


@router_graph.delete('/{graph_id}/graph', openapi_extra=router_meta, name='delete_graph_element', summary='关系视图元素删除')
def delete_graph(graph_id: str,
                 user=Security(permission.security, scopes=['graph:write'])):
    return GraphView.delete_graph_element(graph_id)


# 批量导入导出
@router_graph.post('/export', openapi_extra=router_meta, name='export_graph', summary='关系视图导出')
def export_graph(body: schema.BatchId,
                 session=Depends(db.get_session),
                 user=Security(permission.security, scopes=['graph:read'])):
    return GraphView.export_graph(session, filter_=[GraphView.id.in_(body.ids)] if body.ids else None)


@router_graph.post('/import', openapi_extra=router_meta, name='import_graph', summary='关系视图导入')
def import_graph(body: List[schema.GraphOut],
                 session=Depends(db.get_session),
                 user=Security(permission.security, scopes=['graph:write'])):
    return GraphView.import_graph(session, [item.dict() for item in body])
